#include "custom.h"
#include "ui_custom.h"
#include "dlcw.h"
#include "value.h"
#include "xgmm_custom.h"
#include "dialoglogin.h"
#include <QMessageBox>
#include <QDateTime>
#include <QChartView>
#include <QStandardItem>

extern std::string _name;

Custom::Custom(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Custom)
{
    ui->setupUi(this);
    setWindowTitle("欢迎使用库存系统");
    themodel=new QStandardItemModel(js()+2,3,this);
    ui->tableView->setModel(themodel);
    QString stmp;
    double suuum=Find_sum(_name.c_str());
    if(suuum>=300 && suuum<500)
    {
        ui->cstmrank->setText("会员等级：青铜会员");
        stmp=QString::asprintf("距离升级还需充值：%.2lf",500-suuum);
        ui->cstminstns->setText(stmp);
    }
    else if(suuum>=500&&suuum<1000)
    {
        ui->cstmrank->setText("会员等级：白银会员");
        stmp=QString::asprintf("距离升级还需充值：%.2lf",1000-suuum);
        ui->cstminstns->setText(stmp);
    }
    else if(suuum>=1000)
    {
        ui->cstmrank->setText("会员等级：黄金会员");
        stmp=QString::asprintf("恭喜，您已是满级会员");
        ui->cstminstns->setText(stmp);
    }
    else{
        ui->cstmrank->setText("会员等级：无");
        stmp=QString::asprintf("啊这，您的消费水平过低");
        ui->cstminstns->setText(stmp);
    }
    QStringList headlist;
    headlist<<"时间"<<"金额"<<"收入来源或支出目的";
    themodel->setHorizontalHeaderLabels(headlist);
    //tableview自动适应大小
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setMinimumSectionSize(100);
    ui->tableView->verticalHeader()->setMinimumSectionSize(100);
}

Custom::~Custom()
{
    delete ui;
}

void Custom::on_action_4_triggered() //退出系统
{
    close();
}

void Custom::on_action_triggered() //修改密码
{
    xgmm_custom *xg=new xgmm_custom(this);
    xg->show();
}

void Custom::on_action_2_triggered() //账单明细
{
    FILE* fp;              //文件指针
    int n = 0;
    int i = 12;
    float sum = 0, sum1 = 0, sum2 = 0;
    IAEP head = NULL;
    IAEP p, p2, pr = NULL;
    fp = fopen(_name.c_str(), "ab+");//打开文件，方式为只读
    if (fp == NULL)            //如果打开为空
    {
       // printf("cannot open file\n");
        QMessageBox::warning(this,"err","文件打开失败");
    }
    while (!feof(fp))         // feof函数可以检查文件读写位置标志是否移动到文件末尾，feof(fp)检查fp所指向的文件是否结束，如果是
    {                        //则函数值为真，否则为假，即“!feof(fp)”为真，在while循环中检查 “!feof(fp)”为真，就执行循环体
        n++;
        p = (IAEP)malloc(M);   //向内存申请一段空间
        fread(p, M, 1, fp);      //从fp所指向的文件读入1个大小为M的数据，存储到p所指向的空间中
        if (n == 1)
        {
            head = p;
            p2 = p;
            head->pre = NULL;

        }
        else                //创建双向链表
        {
            pr = p2;          //pr是p的前驱结点
            p->pre = p2;
            p2->next = p;
            p2 = p;
        }
    }
    int num=0;
    fclose(fp);             //关闭fp所指向的文件
    //printf("时     间          金   额                 收入来源或支出目的\n");

    if (pr != NULL)
    {
        pr->next = NULL;
        p = pr;
        QStandardItem *aItem;
        while (p != NULL)
        {

            //printf("%d.%d.%-12d%c%-24f%s\n", p->year, p->month, p->day, p->ch, p->iae, p->source_or_aim);
            aItem=new QStandardItem(QString::asprintf("%d.%d.%d",p->year, p->month, p->day));
            aItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,0,aItem);
            aItem=new QStandardItem(QString::asprintf("%c%f",p->ch, p->iae));
            aItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,1,aItem);
            aItem=new QStandardItem(QString::asprintf("%s",p->source_or_aim));
            aItem->setTextAlignment(Qt::AlignHCenter);
            themodel->setItem(num,2,aItem);

            if (p->ch == '+')
            {
                sum1 += p->iae;
            }
            else if (p->ch == '-')
            {
                sum2 -= p->iae;
            }
            sum = sum1 + sum2;
            i++;
            p = p->pre;
            num++;
        }
        aItem=new QStandardItem(QString::asprintf("总计"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,0,aItem);
        aItem=new QStandardItem(QString::asprintf("总收入"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,1,aItem);
        aItem=new QStandardItem(QString::asprintf("总支出"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,2,aItem);
        num++;
        //printf("总   计： %f    总支出： %f    总收入： %f", sum, sum2, sum1);
        aItem=new QStandardItem(QString::asprintf("%f",sum));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,0,aItem);
        aItem=new QStandardItem(QString::asprintf("%f",sum2));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,1,aItem);
        aItem=new QStandardItem(QString::asprintf("%f",sum1));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,2,aItem);
    }
    else if (pr == NULL)
    {

        //printf("您还没记录任何信息哦~");
        QMessageBox::information(this,"啊哦","您还没记录任何信息哦~~~");
    }
    printf("\n");
}

int Custom::js()
{
    FILE* fp;              //文件指针
    int n = 0;
    IAEP p;
    fp = fopen(_name.c_str(), "ab+");//打开文件，方式为只读
    if (fp == NULL)            //如果打开为空
    {
       // printf("cannot open file\n");
        QMessageBox::warning(this,"err","文件打开失败");
    }
    while (!feof(fp))         // feof函数可以检查文件读写位置标志是否移动到文件末尾，feof(fp)检查fp所指向的文件是否结束，如果是
    {                        //则函数值为真，否则为假，即“!feof(fp)”为真，在while循环中检查 “!feof(fp)”为真，就执行循环体
        n++;
        p = (IAEP)malloc(M);   //向内存申请一段空间
        fread(p, M, 1, fp);      //从fp所指向的文件读入1个大小为M的数据，存储到p所指向的空间中
    }
    fclose(fp);
    return n;
}

void Custom::on_action_3_triggered()//查询库存信息
{
    QStringList headlist;
    headlist<<"产品"<<"产品形式"<<"剩余数量"<<"售价"<<"进货日期"<<"生产日期";
    themodel=new QStandardItemModel(count1+1,6,this);
    themodel->setHorizontalHeaderLabels(headlist);
    ui->tableView->setModel(themodel);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    ui->tableView->horizontalHeader()->setMinimumSectionSize(100);
    ui->tableView->verticalHeader()->setMinimumSectionSize(100);
    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    QStandardItem *aItem;
    int num=0;
    if(count1==0){
        aItem=new QStandardItem(QString::asprintf("无剩余库存"));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(num,1,aItem);
        return;
    }
    struct good *tmp;
    tmp=p;
    for(int i=0;i<count1;i++){
        //"产品"
        aItem=new QStandardItem(QString::asprintf("%s(%s)",tmp->name,tmp->nickname));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,0,aItem);
        //"产品形式"
        std::string tt="盒装";
        if(tmp->typ==1) tt="散装";
        aItem=new QStandardItem(QString::asprintf("%s",tt.c_str()));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,1,aItem);
        //"剩余数量"
        aItem=new QStandardItem(QString::asprintf("%.2lf",tmp->count));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,2,aItem);
        //"售价"
        aItem=new QStandardItem(QString::asprintf("%.2lf",tmp->outprice));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,3,aItem);
        //"进货日期"
        aItem=new QStandardItem(QString::asprintf("%s",tmp->purchasedate));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,4,aItem);
        //"生产日期"
        aItem=new QStandardItem(QString::asprintf("%s",tmp->indate));
        aItem->setTextAlignment(Qt::AlignHCenter);
        themodel->setItem(i,5,aItem);
        tmp=tmp->next;
    }
    tmp=NULL;
}
